package code.google.controller.upnp;

import java.util.ArrayList;

import org.teleal.cling.controlpoint.ControlPoint;
import org.teleal.cling.model.meta.Device;

import code.google.controller.interfaces.IUpnpController;
import code.google.gui.interfaces.IUpnpFragment;
import code.google.model.upnp.UpnpNode;
import code.google.model.util.interfaces.DeviceListObserver;
import code.google.model.util.interfaces.IUpnpNode;

public class UpnpController implements IUpnpController, DeviceListObserver {
	private IUpnpFragment fragment;
	private IUpnpNode node;
	private ControlPoint cPoint;
	private ArrayList<Device> devices = new ArrayList<Device>();
	
	public UpnpController(IUpnpFragment upnpFragment) {
		this.fragment = upnpFragment;
		this.node = (IUpnpNode) UpnpNode.getInstance();
		node.addDeviceListObserver(this);
//		this.cPoint = node.getControlPoint();
	}

	@Override
	public void refreshList() {
		node.refresh();
	}

	@Override
	public void deviceChanged(int operation, Device device) {
		switch (operation) {
		case 0:
			devices.remove(device);
			break;

		case 1:
			int position = devices.indexOf(device);
			if (position >= 0) {
				// Device already in the list, re-set new value at same position
				devices.remove(device);
				devices.add(position, device);
			}else{
				devices.add(device);
				fragment.addToList(device);
			}
			break;
		}
		
	}

}
